package weaver.interfaces.hggd.zjs.Util;

import com.alibaba.fastjson.JSONObject;
import com.wbi.util.DateUtil;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 第三方接口调用日志记录表
 */
public class LogUtil {
    public static void InsertLog(String Url,String param,String result,String dyjg,String requestid) {
        Map<String, String> map = new HashMap<>();
        map.put("dysj",DateUtil.getCurDateTime());
        map.put("tsbw", param);
        map.put("fhbw",result);
        map.put("jkmc", Url);
        map.put("dyjg", dyjg);
        map.put("lcid",requestid);
        addModeInfo(map, "uf_jkrz", 1);
    }


    /**
     * 创建建模的方法
     */
    public static JSONObject addModeInfo(Map<String, String> map, String tablename, int createid) {
        JSONObject returnMap = new JSONObject();
        BaseBean bb = new BaseBean();
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");//设置日期格式
        String modedatacreatedate = sdfDate.format(new Date());
        String modedatacreatetime = sdfTime.format(new Date());
        RecordSet rs = new RecordSet();
        String sql = "";
        int formmodeid = 0;
        sql = "select id from modeinfo where formid=(select id from  workflow_bill where tablename=?)";
        rs.executeQuery(sql, tablename);
        if (rs.next()) {
            formmodeid = rs.getInt("id");
        }
        bb.writeLog("模块数据插入sql:"+map);
        map.put("formmodeid", formmodeid + "");
        map.put("modedatacreater", createid + "");
        map.put("modedatacreatertype", "1");
        map.put("modedatacreatedate", modedatacreatedate);
        map.put("modedatacreatetime", modedatacreatetime);
        String dttablename = "";
        String dttablenvalue = "";
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String fieldname = weaver.general.Util.null2String(entry.getKey());
            String fieldvalue = weaver.general.Util.null2String(entry.getValue());
            dttablename += dttablename == "" ? fieldname : "," + fieldname;
            dttablenvalue += dttablenvalue == "" ? "'" + fieldvalue + "'" : ",'" + fieldvalue + "'";
        }
        if (dttablename != "" && dttablenvalue != "") {
            String sql2 = " insert into " + tablename + "(" + dttablename + ")values (" + dttablenvalue + ") ";
            bb.writeLog("模块数据插入sql:"+sql2);
            boolean flag = rs.executeUpdate(sql2);
            if (flag) {
                sql = "select max(id) from " + tablename;
                rs.executeQuery(sql);
                rs.next();
                int dataid = rs.getInt(1);
                ModeRightInfo modeRightInfo = new ModeRightInfo();
                modeRightInfo.setNewRight(true);
                modeRightInfo.editModeDataShare(createid, formmodeid, dataid);//新建的时候添加共享
                returnMap.put("code", "0");
                returnMap.put("msg", "发送成功");
                returnMap.put("data", dataid + "");
                return returnMap;
            } else {
                returnMap.put("code", "-1");
                returnMap.put("msg", "插入数据失败！");
                return returnMap;
            }
        } else {
            returnMap.put("code", "-1");
            returnMap.put("msg", "输入map对象为空！");
            return returnMap;
        }
    }

}
